<template>
  <div class="permission-container">
    <div class="permission-container">
      <div class="app-container">
        <!-- 表格 -->
        <el-card>
          <div style="text-align: right; margin-bottom: 20px">
            <el-button
              type="primary"
              size="small"
              @click="openDialog"
            >添加权限</el-button>
          </div>
          <el-table border :data="list" row-key="id">
            <el-table-column label="名称" prop="name" />
            <el-table-column label="标识" prop="code" />
            <el-table-column label="描述" prop="description" />
            <el-table-column label="操作">
              <template #default="{ row }">
                <el-button
                  v-if="row.type === 1"
                  size="small"
                  type="text"
                  @click="addPermission(row)"
                >添加权限点</el-button>
                <el-button size="small" type="text" @click="editPermission(row)">查看</el-button>
                <el-button size="small" type="text" @click="delPermission(row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
        <!-- 弹层 -->
        <el-dialog v-loading="isLoading" :visible="showDialog" :title="formData.id?'修改权限':'添加权限'" @close="closeDialog">
          <!-- 表单内容 -->
          <el-form
            ref="addForm"
            label-width="100px"
            :model="formData"
            :rules="rules"
          >
            <el-form-item label="权限名称" prop="name">
              <el-input v-model="formData.name" />
            </el-form-item>
            <el-form-item label="权限标识" prop="code">
              <el-input v-model="formData.code" />
            </el-form-item>
            <el-form-item label="权限描述" prop="description">
              <el-input v-model="formData.description" />
            </el-form-item>
            <el-form-item label="权限启用">
              <el-switch
                v-model="formData.enVisible"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-value="1"
                inactive-value="0"
              />
            </el-form-item>
          </el-form>

          <template #footer>
            <div style="text-align: right">
              <el-button @click="showDialog = false">取消</el-button>
              <el-button type="primary" @click="addConfirm">确定</el-button>
            </div>
          </template>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import { getPermissionListApi, delPermissionApi, getPermissionDetailApi } from '@/api/permission'
import { transListToTreeData } from '@/utils/index'
export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      showDialog: false,
      formData: {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      },
      rules: {
        name: [{ required: true, trigger: 'blur', message: '必填项' }],
        code: [{ required: true, trigger: 'blur', message: '必填项' }],
        description: [{ required: true, trigger: 'blur', message: '必填项' }]
      },
      isLoading: false
    }
  },
  computed: {

  },
  created() {
    this.getPermissionList()
  },
  methods: {
    /**
     * 编辑权限
     */
    async editPermission(row) {
      // console.log(row);
      this.showDialog = true
      this.isLoading = true
      const res = await getPermissionDetailApi(row.id)
      Object.assign(this.formData, res.data)
      this.isLoading = false
    },
    /**
     * 删除权限
     */
    delPermission(row) {
      this.$confirm('此操作将删除文件', '提示').then(async(res) => {
        await delPermissionApi(row.id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        // 重新获取数据
        this.getPermissionList()
      }).catch((err) => {
        console.log(err)
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    /**
     * 添加权限点
     */
    addPermission(row) {
      this.formData.pid = row.id
      this.formData.type = 2
      this.showDialog = true
    },
    /**
     * 点击确认按钮
     */
    addConfirm() {
      this.$refs.addForm.validate((flag) => {
        if (!flag) {
          return
        }
        console.log(this.formData)
        // 如果formData.id 有值，就编辑接口，否就添加接口
        this.showDialog = false
      })
    },
    /**
     * 关闭弹框时
     */
    closeDialog() {
      this.$refs.addForm.resetFields()
      this.formData = {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      }
      this.formData.id = ''
      console.log(this.formData.id)
      this.showDialog = false
    },
    /**
     * 开启弹窗,添加权限页
     */
    openDialog() {
      // 添加数据，表示权限点类型
      this.formData.type = 1
      this.formData.pid = '0'
      this.showDialog = true
    },
    /**
     * 得到权限名单
     */
    async getPermissionList() {
      const { data } = await getPermissionListApi()
      this.list = transListToTreeData(data, '0')
    }
  }
}
</script>

  <style>
</style>
